package com.zjp.controller;

import com.zjp.pojo.User;
import com.zjp.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.IOException;


/**
 * 用户登录注册
 */
@Slf4j
@Controller
public class UserController {
    @Autowired
    private UserService userService;

    //注册
    @RequestMapping("/doRegister")
    public String doRegister(User user, Model model) {
        //判断注册信息是否为空，为空返回登录页面
        System.out.println("==========" + user);
        if (user.getPassword() != null && !user.getPassword().isEmpty()) {
            //对密码进行加密
            String md5Password = DigestUtils.md5DigestAsHex(user.getPassword().getBytes());
            user.setPassword(md5Password);
        } else {
            // 如果密码为空，则不应该继续注册流程
            model.addAttribute("errorMsg", "密码不能为空");
            return "login";
        }
        // 判断注册信息是否为空，为空返回登录页面
        System.out.println("==========" + user);
        if (user.getName() == null || user.getRoleid() == null) {
            model.addAttribute("errorMsg", "注册信息不完整");
            return "login";
        }
        //设置健康码状态为：0 绿码，1 黄码，2 红码。
        user.setHealthStatus(0);
        //添加默认头像
        user.setPic("mrtx.jpg");
        int i = userService.doRegiseter(user);
        if (i > 0) {
            model.addAttribute("errorMsg", "注册成功");
        } else {
            model.addAttribute("errorMsg", "用户已存在,注册失败");
        }
        return "login";
    }

    /**
     * 用户登录
     */
    @RequestMapping("/doLogin")
        public String doLogin(String name, String password, String inputCode, HttpSession session, Model model) {
        if (inputCode == null) {
            return "login";
        }
        if (!inputCode.equals(session.getAttribute("vrifyCode"))) {
            model.addAttribute("errorMsg", "验证码错误");
            return "login";
        }
        if (name == null || password == null) {   //防止刷新直接访问doLogin
            return "redirect:/login.html";
        }
        User user = userService.doLogin(name, password);
        if (user != null) {
            session.setAttribute("user", user);
            session.setMaxInactiveInterval(-1);  //设置session不失效
            return "index";
        } else {
            model.addAttribute("errorMsg", "用户名或密码错误");
            return "login";
        }
    }

    /**
     * 修改用户信息
     */
    @PostMapping("/updateUserMassage")
    @ResponseBody
    public String updateUserMassage(User user, @PathVariable(value = "uploadpic", required = false) MultipartFile uploadpic, HttpSession session) throws IOException {
        if (uploadpic.getSize() > 0) {
            //获取图片上传路径
            String paths = "D:/java软件/项目/shixunxiangmu/shixun/new_vacc/src/main/resources/static/images";
            File newPic = new File(paths, uploadpic.getOriginalFilename());
            uploadpic.transferTo(newPic);  //存储图片
            user.setPic(uploadpic.getOriginalFilename());
        } else {//如果没有修改图片就使用原先的图片
            User u = (User) session.getAttribute("user");
            user.setPic(u.getPic());
        }
        int i = userService.updateUserMassage(user);
        if (i > 0) {
            return "SUCCESS";
        }
        return "FAIL";
    }

    /**
     * 退出登录
     */
    @RequestMapping("/loginOut")
    public String loginOut(HttpSession session) {
        session.invalidate();   //清空session
        return "login";  //返回登录页面
    }
}






